package fun.easycode.webterminal.common.jsch;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.Session;
import fun.easycode.webterminal.common.contract.CheckException;
import fun.easycode.webterminal.dao.Machine;
import fun.easycode.webterminal.dao.MachineMapper;
import fun.easycode.webterminal.dao.MachineType;
import lombok.Getter;

public class JschJumpSession {

    private final String machineId;

    @Getter
    private Session session;
    @Getter
    private Session jumpSession;

    public JschJumpSession(String machineId){
        this.machineId = machineId;
    }

    public void connect(){
        MachineMapper mapper = SpringUtil.getBean(MachineMapper.class);

        Machine machine = mapper.selectById(this.machineId);

        if(machine == null){
            throw new CheckException("找不到主机信息");
        }

        if(machine.getIsJump()){
            Machine jumpMachine = mapper.selectById(machine.getJumpMachineId());

            // 根据类型选择ssh认证方式
            if(jumpMachine.getType() == MachineType.PASSWORD){
                this.jumpSession = JschUtil.openSession(jumpMachine.getIp(), jumpMachine.getPort(), jumpMachine.getUsername(), jumpMachine.getPassword());
            }else{
                this.jumpSession = JschUtil.openSession(jumpMachine.getIp(), jumpMachine.getPort(), jumpMachine.getUsername(), jumpMachine.getPublicKey().getBytes(), null);
            }

            // 生成随机端口
            int randomPort = JschUtil.generateLocalPort();

            boolean r = JschUtil.bindPort(this.jumpSession, machine.getIp(), machine.getPort(), randomPort);

            if(!r){
                this.disconnect();
                throw new CheckException("隧道启动失败");
            }


            // 根据类型选择ssh认证方式
            if(machine.getType() == MachineType.PASSWORD){
                this.session = JschUtil.openSession("127.0.0.1", randomPort, machine.getUsername(), machine.getPassword());
            }else{
                this.session = JschUtil.openSession("127.0.0.1", randomPort, machine.getUsername(), machine.getPublicKey().getBytes(), null);
            }

        }else{
            // 根据类型选择ssh认证方式
            if(machine.getType() == MachineType.PASSWORD){
                this.session = JschUtil.openSession(machine.getIp(), machine.getPort() , machine.getUsername(), machine.getPassword());
            }else{
                this.session = JschUtil.openSession(machine.getIp(), machine.getPort(), machine.getUsername(), machine.getPublicKey().getBytes(), null);
            }
        }

    }

    public boolean isConnected(){
        if(this.jumpSession == null){
            return this.session.isConnected();
        }else{
            return this.jumpSession.isConnected() && this.session.isConnected();
        }
    }

    public void disconnect(){
        if(this.session != null){
            this.session.disconnect();
        }
        if(this.jumpSession != null){
            this.jumpSession.disconnect();
        }
    }
}
